package com.yunfeng.aolin.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yunfeng.aolin.entity.Adhesive;
import com.yunfeng.aolin.service.AdhesiveService;
import com.yunfeng.common.log.Log;
import com.yunfeng.common.log.LogMessageObject;
import com.yunfeng.common.log.impl.LogUitl;
import com.yunfeng.common.util.AjaxObject;
import com.yunfeng.common.util.FileUtils;
import com.yunfeng.common.util.Page;

@Controller
@RequestMapping("/management/produce/adhesive")
public class AdhesiveController {

	@Autowired
	private AdhesiveService adhesiveService;

	private static final String CREATE = "management/produce/adhesive/create";
	private static final String UPDATE = "management/produce/adhesive/update";
	private static final String LIST = "management/produce/adhesive/list";
	private static final String VIEW = "management/produce/adhesive/view";

	@InitBinder
	public void dataBinder(WebDataBinder dataBinder) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(df,
				true));
	}

	@RequiresPermissions("Adhesive:save")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String preCreate(Map<String, Object> map) {
		return CREATE;
	}

	@Log(message = "添加了id={0}背胶记录。")
	@RequiresPermissions("Adhesive:save")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public @ResponseBody
	String create(@Valid Adhesive adhesive) {
		adhesiveService.saveOrUpdate(adhesive);

		LogUitl.putArgs(LogMessageObject.newWrite().setObjects(
				new Object[] { adhesive.getId() }));
		return AjaxObject.newOk("背胶记录添加成功！").toString();
	}

	@ModelAttribute("preloadAdhesive")
	public Adhesive preload(
			@RequestParam(value = "id", required = false) Long id) {
		if (id != null) {
			Adhesive adhesive = adhesiveService.get(id);
			return adhesive;
		}
		return null;
	}

	@RequiresPermissions("Adhesive:edit")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String preUpdate(@PathVariable Long id, Map<String, Object> map) {
		Adhesive adhesive = adhesiveService.get(id);
		map.put("adhesive", adhesive);
		return UPDATE;
	}

	@Log(message = "修改了id={0}背胶记录的信息。")
	@RequiresPermissions("Adhesive:edit")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody
	String update(@Valid @ModelAttribute("preloadAdhesive") Adhesive adhesive) {
		adhesiveService.saveOrUpdate(adhesive);

		LogUitl.putArgs(LogMessageObject.newWrite().setObjects(
				new Object[] { adhesive.getId() }));
		return AjaxObject.newOk("背胶记录修改成功！").toString();
	}

	@Log(message = "删除了id={0}背胶记录。")
	@RequiresPermissions("Adhesive:delete")
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public @ResponseBody
	String delete(@PathVariable Long id) {
		adhesiveService.delete(id);

		LogUitl.putArgs(LogMessageObject.newWrite().setObjects(
				new Object[] { id }));
		return AjaxObject.newOk("背胶记录删除成功！").setCallbackType("").toString();
	}

	@Log(message = "批量删除了id={0}背胶记录。")
	@RequiresPermissions("Adhesive:delete")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody
	String deleteMany(Long[] ids) {
		for (int i = 0; i < ids.length; i++) {
			Adhesive adhesive = adhesiveService.get(ids[i]);
			adhesiveService.delete(adhesive.getId());
		}

		LogUitl.putArgs(LogMessageObject.newWrite().setObjects(
				new Object[] { Arrays.toString(ids) }));
		return AjaxObject.newOk("背胶记录删除成功！").setCallbackType("").toString();
	}

	@RequiresPermissions("Adhesive:view")
	@RequestMapping(value = "/list", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String list(Page page, String keywords, Map<String, Object> map) {
		List<Adhesive> adhesives = null;
		if (StringUtils.isNotBlank(keywords)) {
			adhesives = adhesiveService.findByLiaohao(page, keywords);
		} else {
			adhesives = adhesiveService.findAll(page);
		}

		map.put("page", page);
		map.put("adhesives", adhesives);
		map.put("keywords", keywords);

		return LIST;
	}

	@RequiresPermissions("Adhesive:view")
	@RequestMapping(value = "/view/{id}", method = { RequestMethod.GET })
	public String view(@PathVariable Long id, Map<String, Object> map) {
		Adhesive adhesive = adhesiveService.get(id);
		map.put("adhesive", adhesive);
		return VIEW;
	}

	@RequiresPermissions("Adhesive:exportExcel")
	@RequestMapping(value = "/exportExcel", method = { RequestMethod.GET,
			RequestMethod.POST })
	public void exportExcel(HttpServletResponse response) throws Exception {
		List<Adhesive> adhesives = adhesiveService.findAll();
		FileUtils.exportExcel(response, adhesives);
	}

}
